package com.shelpe.services.sycm.utils;


import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class HttpClient {

//	static HttpClient instance;
//	
//	static HttpClient getInstance(){
//		if(instance == null){
//			instance = new HttpClient();
//		}
//		return instance;
//	}
	
	public static String get(String uri, String cookie) throws ClientProtocolException, IOException{
		uri = uri.replace("|", "%7C");
		HttpGet get = new HttpGet(uri);
		get.setHeader("cookie", cookie);
		CloseableHttpClient client = HttpClients.createDefault();
		CloseableHttpResponse response = client.execute(get);
		List<String> lines = IOUtils.readLines(response.getEntity().getContent(), "UTF-8");
		StringBuffer sb = new StringBuffer();
		for(String line : lines){
			sb.append(line);
		}
		return sb.toString();
	}
//	
//	public static void main(String[] args) {
//		try {
//			String html = new TestHttpClient()
//					.get("https://bda.sycm.taobao.com/flow/getSourceTop.jsonp?sycmToken=9c1466cb2&dateRange=2016-08-20&dateType=recent1&top=10");
//			System.out.println("html:"+html);
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}

}
